/**
 * 
 */
package jdbcts.resultSet;

import java.sql.*;

import jdbcts.SuperTest;
import jdbcts.common.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.sun.cts.harness.EETest.Fault;

/**
 * @author Administrator
 * 
 */
public class ResultSetClient1 extends SuperTest
{

    // private static final String testName = "jdbc.ee.resultSet.resultSet1";

    // Naming specific member variables
    // private CTSNamingContextInterface jc = null;

    // Harness requirements

    private transient static Connection xmlCon = null;

    private static Statement stmt = null;

//     private DataSource ds1 = null;

    private static ResultSet rs = null;

     private static String drManager = null;

    // private String sqlStmt = null;

     private static dbSchema dbSch = null;

    // private int rscount = 0;

    // private ResultSet rs1 = null;

    private static String query1 = null;

    private int rowMaxVal = 0;

    private static String query = null;

    // private static String query =
    // private static String url = null;

    @BeforeClass
    public static void setup() throws Exception
    {
	try
	{
	    try
	    {
//		drManager = p.getProperty("DriverManager", "");
		drManager = isDRManager;
		if (drManager.length() == 0)
		    throw new Fault("Invalid DriverManager Name");

//		String fTableName = p.getProperty("ftable", "");

		/* String query = "SELECT COF_NAME, PRICE FROM " + fTableName; */
//		query = p.getProperty("SelCoffeeAll", "");
		query = queryAll;
		if (query.length() == 0)
		    throw new Fault("Invalid SQL Statement ");
		logMsg("Query to select All the rows :" + query);

//		query1 = p.getProperty("SelCoffeeNull", "");
		query1 = queryNull;

		/* query1="SELECT * FROM " + fTableName + " WHERE TYPE_ID=0"; */
		if (query1.length() == 0)
		    throw new Fault("Invalid SQL Statement ");
		logMsg("Query to select Null rows :" + query1);

		if (drManager.equals("yes"))
		{
		    logMsg("Using DriverManager");
		    DriverManagerConnection dmCon = new DriverManagerConnection();
//		    xmlCon = dmCon.getConnection(p);
		    xmlCon = dmCon.getConnection(null);
		}
		else
		{
		    logMsg("Using DataSource");
		    DataSourceConnection dsCon = new DataSourceConnection();
//		    coffeeCon = dsCon.getConnection(p);
		    xmlCon = dsCon.getConnection(null);
		}
		dbSch = new dbSchema();
//		dbSch.createData(p, coffeeCon);
		stmt = xmlCon.createStatement(/*
						  * ResultSet.TYPE_SCROLL_INSENSITIVE
						  * ,ResultSet.CONCUR_READ_ONLY
						  */);
		rs = stmt.executeQuery(query.trim());
	    }
	    catch (SQLException ex)
	    {
		logErr("SQL Exception : " + ex.getMessage());
	    }
	}
	catch (Exception e)
	{
	    logErr("Setup Failed!");
	}
    }

    /*
     * This method is to calculate the number of rows in a ResultSet. Call the
     * rsCount(ResultSet rs) method. It Should return an Integer value
     */
    // public int rsCount(ResultSet rs) throws Fault
    // {
    // int count = 0;
    // try
    // {
    // while (rs.next())
    // {
    // count++;
    // }
    // }
    // catch (SQLException sqle)
    // {
    // logErr("SQL Exception :" + sqle.getMessage());
    // //throw new Fault("Call to rsCount is Failed!", sqle);
    // }
    // return count;
    // }
    /*
     * @testName: testGetConcurrency
     * 
     * @assertion: The ResultSet object contains the results of executing an SQL
     * Query. ResultSets may hava different levels of functionality (ResultSet
     * Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, TYPE_SCROLL_INSENSITIVE)
     * and update capabilities (ResultSet Concurrency). (See section 26.1 of
     * JDBC 2.0 API Reference & Tutorial second edition). The JDBC driver must
     * provide full support for TYPE_SCROLL_INSENSITIVE result sets. (See
     * section 6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
     * The getCocurrency() returns the concurrency mode of this ResultSet
     * object. The concurrency used is determined by the Statement object that
     * created the result set.
     * 
     * @test_Strategy: Get a ResultSet object by executing the query and call
     * the getConcurrency() method on that resultset object. It should return an
     * Integer value and the value should be equal to any of the values
     * CONCUR_READ_ONLY or CONCUR_UPDATABLE which are defined in the Resutset
     * interface.
     */
    @Test
    public void testGetConcurrency() throws Exception
    {
	try
	{
	    // invoke on the getConcurrency
	    logMsg("Calling getConcurrency on ResultSet");
	    int concurrencyValue = rs.getConcurrency();
	    if (concurrencyValue == ResultSet.CONCUR_READ_ONLY)
	    {
		logMsg("getConcurrency method returns ResultSet.CONCUR_READ_ONLY ");
	    }
	    else
		if (concurrencyValue == ResultSet.CONCUR_UPDATABLE)
		{
		    logMsg("getConcurrency method returns ResultSet.CONCUR_UPDATABLE");
		}
		else
		{
		    logErr(" getConcurrency method returns a invalid value");
		    // throw new Fault("Call to getConcurrency is Failed");
		    throw new Exception("Call to getConcurrency is Failed");
		}
	}
	catch (SQLException sqle)
	{
	    logErr("SQL Exception " + sqle.getMessage());
	    // throw new Fault("Call to getConcurrency is Failed!"
	    // , sqle);
	    throw new Exception("Call to getConcurrency is Failed!", sqle);
	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    // throw new Fault("Call to getConcurrency is Failed!"
	    // , e);
	    throw new Exception("Call to getConcurrency is Failed!", e);
	}
    }

    /*
     * @testName: testGetFetchDirection
     * 
     * @assertion: The ResultSet object contains the results of executing an SQL
     * Query. ResultSets may hava different levels of functionality (ResultSet
     * Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, TYPE_SCROLL_INSENSITIVE)
     * and update capabilities (ResultSet Concurrency). (See section 26.1 of
     * JDBC 2.0 API Reference & Tutorial second edition). The JDBC driver must
     * provide full support for TYPE_SCROLL_INSENSITIVE result sets. (See
     * section 6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
     * The getFetchDirection() method returns the fetch direction for this
     * ResultSet object.
     * 
     * @test_Strategy: Get a ResultSet object by executing a query and call the
     * getFetchDirection() method.It should return an Integer value and the
     * value should be equal to any of the values FETCH_FORWARD or FETCH_REVERSE
     * or FETCH_UNKNOWN which are defined in the Resultset interface.
     */
    @Test
    public void testGetFetchDirection() throws Exception
    {
	try
	{
	    // invoke on the getFetchDirection
	    logMsg("Calling getFetchDirection on ResultSet");
	    int fetchDirectionValue = rs.getFetchDirection();
	    if (fetchDirectionValue == ResultSet.FETCH_FORWARD)
	    {
		logMsg("getFetchDirection method returns ResultSet.FETCH_FORWARD ");
	    }
	    else
		if (fetchDirectionValue == ResultSet.FETCH_REVERSE)
		{
		    logMsg("getFetchDirection method returns ResultSet.FETCH_REVERSE");
		}
		else
		    if (fetchDirectionValue == ResultSet.FETCH_UNKNOWN)
		    {
			logMsg("getFetchDirection method returns ResultSet.FETCH_UNKNOWN");
		    }
		    else
		    {
			logErr(" getFetchDirection method returns a invalid value");
			// throw new
			// Fault("Call to getFetchDirection is Failed");
			throw new Exception(
				"Call to getFetchDirection is Failed");
		    }
	}
	catch (SQLException sqle)
	{
	    logErr("SQL Exception " + sqle.getMessage());
	    // throw new Fault("Call to getFetchDirection is Failed!", sqle);
	    throw new Exception("Call to getFetchDirection is Failed!", sqle);
	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    // throw new Fault("Call to getFetchDirection is Failed!"
	    // , e);
	    throw new Exception("Call to getFetchDirection is Failed!", e);
	}
    }

    /*
     * @testName: testGetFetchSize
     * 
     * @assertion: The ResultSet object contains the results of executing an SQL
     * Query. ResultSets may hava different levels of functionality (ResultSet
     * Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, TYPE_SCROLL_INSENSITIVE)
     * and update capabilities (ResultSet Concurrency). (See section 26.1 of
     * JDBC 2.0 API Reference & Tutorial second edition). The JDBC driver must
     * provide full support for TYPE_SCROLL_INSENSITIVE result sets. (See
     * section 6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
     * The getFetchSize() method returns the fetch size for this ResultSet
     * object. i.e.,the number of rowsthat should be fetched from the database
     * when more rows are needed for this ResultSet object.
     * 
     * @test_Strategy: Get a ResultSet object and call the getFetchSize() method
     * It should return a Integer value which should be greater than or equal to
     * zero.
     */
    @Test
    public void testGetFetchSize() throws Exception
    {
	try
	{
	    // invoke on the getFetchSize
	    logMsg("Calling getFetchSize on ResultSet");
	    int fetchSizeValue = rs.getFetchSize();
	    if (fetchSizeValue >= 0)
	    {
		logMsg("getFetchSize method returns :" + fetchSizeValue);
	    }
	    else
	    {
		logMsg(" getFetchSize method returns a invalid value");
	    }
	}
	catch (SQLException sqle)
	{
	    logErr("SQL Exception " + sqle.getMessage());
	    // throw new Fault("Call to getFetchSize is Failed!", sqle);
	    throw new Exception("Call to getFetchSize is Failed!", sqle);
	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    // throw new Fault("Call to getFetchSize is Failed!", e);
	    throw new Exception("Call to getFetchSize is Failed!", e);
	}
    }

    /*
     * @testName: testGetType
     * 
     * @assertion: The ResultSet object contains the results of executing an SQL
     * Query. ResultSets may hava different levels of functionality (ResultSet
     * Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, TYPE_SCROLL_INSENSITIVE)
     * and update capabilities (ResultSet Concurrency). (See section 26.1 of
     * JDBC 2.0 API Reference & Tutorial second edition). The JDBC driver must
     * provide full support for TYPE_SCROLL_INSENSITIVE result sets. (See
     * section 6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
     * The getType() method returns the type of this ResultSet object. The type
     * is determined by the Statement object that created the result set.
     * 
     * @test_Strategy: Get a ResultSet object and call the getType() method on
     * that object. It should return an Integer value and the value should be
     * equal to any of the values TYPE_FORWARD_ONLY or TYPE_SCROLL_INSENSITIVE
     * or TYPE_SCROLL_SENSITIVE which are defined in the Resultset interface.
     */
    @Test
    public void testGetType() throws Exception
    {
	try
	{
	    // invoke on the getType
	    logMsg("Calling getType on ResultSet");
	    int typeValue = rs.getType();
	    if (typeValue == ResultSet.TYPE_FORWARD_ONLY)
	    {
		logMsg("getType method returns ResultSet.TYPE_FORWARD_ONLY ");
	    }
	    else
		if (typeValue == ResultSet.TYPE_SCROLL_INSENSITIVE)
		{
		    logMsg("getType method returns ResultSet.TYPE_SCROLL_INSENSITIVE");
		}
		else
		    if (typeValue == ResultSet.TYPE_SCROLL_SENSITIVE)
		    {
			logMsg("getType method returns ResultSet.TYPE_SCROLL_SENSITIVE");
		    }
		    else
		    {
			logErr(" getType method returns a invalid value");
			// throw new Fault("Call to getType is Failed");
			throw new Exception("Call to getType is Failed");
		    }
	}
	catch (SQLException sqle)
	{
	    logErr("SQL Exception " + sqle.getMessage());
	    // throw new Fault("Call to getType is Failed!", sqle);
	    throw new Exception("Call to getType is Failed!", sqle);
	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    // throw new Fault("Call to getType is Failed!", e);
	    throw new Exception("Call to getType is Failed!", e);
	}
    }

    /*
     * @testName: testSetFetchSize01
     * 
     * @assertion: The ResultSet object contains the results of executing an SQL
     * Query. ResultSets may hava different levels of functionality (ResultSet
     * Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, TYPE_SCROLL_INSENSITIVE)
     * and update capabilities (ResultSet Concurrency). (See section 26.1 of
     * JDBC 2.0 API Reference & Tutorial second edition). The JDBC driver must
     * provide full support for TYPE_SCROLL_INSENSITIVE result sets. (See
     * section 6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
     * setFetchSize(int rows) gives the JDBC driver a hint as to the number of
     * rows that should be fetched from the database when more rows are needed
     * for this ResultSet object. If the fetch size specified is zero, the JDBC
     * driver ignores the value and is free to make its own best guess as to
     * what the fetch size should be. The default value is set by the Statement
     * object that created the result set. The fetch size may be changed at any
     * time.
     * 
     * @test_Strategy: Get a ResultSet object by executing a query and call the
     * setFetchSize(int rows). Set the value of rows to zero. The JDBC driver is
     * free to make its own best guess as to what the fetch size should be. Then
     * call getFetchSize() method.
     */
    @Test
    public void testSetFetchSize01() throws Exception
    {
	try
	{
	    // invoke on the setFetchSize
	    logMsg("Calling setFetchSize on ResultSet");
	    rs.setFetchSize(0);
	    int fetchSizeValue = rs.getFetchSize();
	}
	catch (SQLException sqle)
	{
	    logErr("SQL Exception " + sqle.getMessage());
	    // throw new Fault("Call to setFetchSize is Failed!", sqle);
	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    // throw new Fault("setFetchSize Failed!", e);
	    throw new Exception("setFetchSize Failed!", e);
	}
    }

    /*
     * @testName: testSetFetchSize02
     * 
     * @assertion: The ResultSet object contains the results of executing an SQL
     * Query. ResultSets may hava different levels of functionality (ResultSet
     * Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, TYPE_SCROLL_INSENSITIVE)
     * and update capabilities (ResultSet Concurrency). (See section 26.1 of
     * JDBC 2.0 API Reference & Tutorial second edition). The JDBC driver must
     * provide full support for TYPE_SCROLL_INSENSITIVE result sets. (See
     * section 6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
     * setFetchSize(int rows) gives the JDBC driver a hint as to the number of
     * rows that should be fetched from the database when more rows are needed
     * for this ResultSet object. If the fetch size specified is zero, the JDBC
     * driver ignores the value and is free to make its own best guess as to
     * what the fetch size should be. The default value is set by the Statement
     * object that created the result set. The fetch size may be changed at any
     * time.
     * 
     * @test_Strategy: Get a ResultSet object and call the setFetchSize(int
     * rows) on that obect. Call Statement's getMaxRows() before calling
     * setFetchSize() method and pass the returned value from getMaxRows()
     * method as the argument to the setFetchSize() method. Then call
     * getFetchSize() method to check whether the returned value is the same
     * that has been set.
     */
    @Test
    public void testSetFetchSize02() throws Exception
    {
	try
	{
	    // Setting the maxRows to 5 to make sure that we have a non-zero
	    // value
	    // for maxrows

	    Statement stmt = xmlCon.createStatement();
	    stmt.setMaxRows(5);

	    ResultSet rs = stmt.executeQuery(query.trim());

	    // invoke on the setFetchSize
	    // rowMaxVal is the maximum Rows that ResultSet can contain
	    rowMaxVal = stmt.getMaxRows();

	    logMsg("Calling setFetchSize() on ResultSet");
	    rs.setFetchSize(rowMaxVal);
	    int fetchSizeValue = rs.getFetchSize();

	    if (fetchSizeValue == rowMaxVal)
	    {
		logMsg("setFetchSize method sets number of rows that has been specified :"
			+ fetchSizeValue);
	    }
	    else
	    {
		logErr("setFetchSize method does not set number of rows that has been specified");
		// throw new Fault("Call to setFetchSize is Failed");
		throw new Exception("Call to setFetchSize is Failed");
	    }
	}
	catch (SQLException sqle)
	{
	    logErr("SQL Exception " + sqle.getMessage());
	    // throw new Fault("Call to setFetchSize is Failed!", sqle);
	    throw new Exception("Call to setFetchSize is Failed!", sqle);
	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    // throw new Fault("setFetchSize Failed!", e);
	    throw new Exception("setFetchSize Failed!", e);
	}
    }

    /*
     * @testName: testSetFetchSize03
     * 
     * @assertion: The ResultSet object contains the results of executing an SQL
     * Query. ResultSets may hava different levels of functionality (ResultSet
     * Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, TYPE_SCROLL_INSENSITIVE)
     * and update capabilities (ResultSet Concurrency). (See section 26.1 of
     * JDBC 2.0 API Reference & Tutorial second edition). The JDBC driver must
     * provide full support for TYPE_SCROLL_INSENSITIVE result sets. (See
     * section 6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
     * setFetchSize(int rows) gives the JDBC driver a hint as to the number of
     * rows that should be fetched from the database when more rows are needed
     * for this ResultSet object. If the fetch size specified is zero, the JDBC
     * driver ignores the value and is free to make its own best guess as to
     * what the fetch size should be. The default value is set by the Statement
     * object that created the result set. The fetch size may be changed at any
     * time.
     * 
     * @test_Strategy: Get a ResultSet object and call the setFetchSize(int
     * rows) on that object with the value greater than the Statement's maximum
     * possible rows as returned by getMaxRows() method and it should throw
     * SQLException. In case if the getMaxRows() method returns 0 which means
     * unlimited rows then appropriate message is displayed and test passes
     */
    @Test
    public void testSetFetchSize03() throws Exception
    {
	boolean flag = false;
	try
	{
	    // invoke on the setFetchSize
	    // rowMaxVal is the maximum Rows that ResultSet can contain
	    rowMaxVal = stmt.getMaxRows();
	    logMsg("Calling setFetchSize on ResultSet");
	    try
	    {
		if (rowMaxVal > 0)
		{
		    rs.setFetchSize(rowMaxVal + 2);
		}
		else
		    if (rowMaxVal == 0)
		    {
			logMsg("Resultset supports unlimited number of rows");
			flag = true;
		    }
	    }
	    catch (SQLException sqe)
	    {
		flag = true;
	    }
	    if (flag)
	    {
		logMsg("setFetchSize method does not set the size greater than the Maximum Rows that ResultSet can contain");
	    }
	    else
	    {
		logErr("setFetchSize method sets the size greater than the Maximum Rows that ResultSet can contain ");
		// throw new Fault("Call to setFetchSize is Failed!");
		throw new Exception("Call to setFetchSize is Failed!");
	    }

	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    // throw new Fault("setFetchSize Failed!", e);
	    throw new Exception("setFetchSize Failed!", e);
	}
    }

    /*
     * @testName: testSetFetchSize04
     * 
     * @assertion: The ResultSet object contains the results of executing an SQL
     * Query. ResultSets may hava different levels of functionality (ResultSet
     * Types TYPE_FORWARD_ONLY, TYPE_SCROLL_SENSITIVE, TYPE_SCROLL_INSENSITIVE)
     * and update capabilities (ResultSet Concurrency). (See section 26.1 of
     * JDBC 2.0 API Reference & Tutorial second edition). The JDBC driver must
     * provide full support for TYPE_SCROLL_INSENSITIVE result sets. (See
     * section 6.2.2.3 of Java2 Platform Enterprise Edition Specification v1.2)
     * setFetchSize(int rows) gives the JDBC driver a hint as to the number of
     * rows that should be fetched from the database when more rows are needed
     * for this ResultSet object. If the fetch size specified is zero, the JDBC
     * driver ignores the value and is free to make its own best guess as to
     * what the fetch size should be. The default value is set by the Statement
     * object that created the result set. The fetch size may be changed at any
     * time.
     * 
     * @test_Strategy: Get a ResultSet object and call the setFetchSize(int
     * rows) on that object. And try to set a negative value .It should throw
     * SQLException.
     */
    @Test
    public void testSetFetchSize04() throws Exception
    {
	boolean flag = false;
	try
	{
	    // invoke on the setFetchSize
	    logMsg("Calling setFetchSize(-1) on ResultSet");
	    try
	    {

		rs.setFetchSize(-1);
	    }
	    catch (SQLException sqe)
	    {
		flag = true;
	    }
	    if (flag)
	    {
		logMsg("setFetchSize method does not set the size as negative ");
	    }
	    else
	    {
		logErr("setFetchSize method sets the size as negative ");
		// throw new Fault("Call to setFetchSize is Failed!");
		throw new Exception("Call to setFetchSize is Failed!");
	    }
	}
	catch (Exception e)
	{
	    logErr("Unexpected exception " + e.getMessage());
	    // throw new Fault("setFetchSize Failed!", e);
	    throw new Exception("setFetchSize Failed!", e);
	}
    }

    /* cleanup */
    @AfterClass
    public static void cleanup() throws Exception
    {
	try
	{
	    stmt.close();
	    // Close the database
	    // dbSch.destroyData(coffeeCon);
	     dbSch.dbUnConnect(xmlCon);
//	    dbUnConnect(xmlCon);
	    logMsg("Cleanup ok;");
	}
	catch (Exception e)
	{
	    logErr("An error occurred while closing the database connection");
	}
    }
}
